package com.enlf.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.enlf.dto.R;
import com.enlf.entity.Shop;
import com.enlf.service.IShopService;
import com.enlf.utils.AliOssUtil;
import com.enlf.utils.SystemConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * _*_ coding : utf-8 _*_
 *
 * @Time : 2024/3/31 11:09
 * @Author : 计201任思伟
 * @File : AdminShopController
 * @Project : enjoy-life
 */
@RestController
@RequestMapping("/admin/shop")
public class AdminShopController {

    @Resource
    public IShopService shopService;
    @Resource
    private AliOssUtil aliOssUtil;
    /**
     * 新增商铺信息
     * @param shop 商铺数据
     * @return 商铺id
     */
    @PostMapping
    public R saveShop(@RequestBody Shop shop) {
        // 写入数据库
        shopService.save(shop);
        // 返回店铺id
        return R.OK(shop.getId());
    }

    /**
     * 分页查询商铺信息
     * @param current 页码
     * @return 商铺列表
     */
    @GetMapping("/shopList")
    public R queryShopByType(
            @RequestParam(value = "current", defaultValue = "1") Integer current
    ) {
        // 根据类型分页查询
        Page<Shop> page = shopService.query()
                .page(new Page<>(current, SystemConstants.DEFAULT_PAGE_SIZE_TEN));
        // 返回数据
        Map<String,Object> result = new HashMap<>();
        result.put("total",page.getTotal());
        result.put("list",page.getRecords());
        return R.OK(result);
    }

    /**
     * 根据id查询商铺信息
     * @param id 商铺id
     * @return 商铺详情数据
     */
    @GetMapping("/info/{id}")
    public R queryShopById(@PathVariable("id") Long id) {
        return R.OK(shopService.queryById(id));
    }

    /**
     * 更新商铺信息
     * @param shop 商铺数据
     * @return 无
     * 双写操作 先缓存后查数据库
     */
    @PutMapping("/editshop")
    public R updateShop(Shop shop) {
        // 写入数据库
        shopService.update(shop);
        return R.OK();
    }

    /**
     * 根据商铺类型分页查询商铺信息
     * @param typeId 商铺类型
     * @param current 页码
     * @return 商铺列表
     */
    @GetMapping("/of/type")
    public R queryShopByType(
            @RequestParam("typeId") Integer typeId,
            @RequestParam(value = "current", defaultValue = "1") Integer current
    ) {
        // 根据类型分页查询
        Page<Shop> page = shopService.query()
                .eq("type_id", typeId)
                .page(new Page<>(current, SystemConstants.DEFAULT_PAGE_SIZE_FIVE));
        // 返回数据
        return R.OK(page.getRecords());
    }

    /**
     * 根据商铺名称关键字分页查询商铺信息
     * @param name 商铺名称关键字
     * @param current 页码
     * @return 商铺列表
     */
    @GetMapping("/of/name")
    public R queryShopByName(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "current", defaultValue = "1") Integer current
    ) {
        // 根据类型分页查询
        Page<Shop> page = shopService.query()
                .like(StrUtil.isNotBlank(name), "name", name)
                .page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 返回数据
        Map<String,Object> result = new HashMap<>();
        result.put("total",page.getTotal());
        result.put("list",page.getRecords());
        return R.OK(result);
    }
    @PostMapping("/upload")
    public R upload(MultipartFile file){
        //MultipartFile file接收文件路径
        //将文件上传到阿里云  阿里云使用uuid存储
        try {
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //截取原始文件名的后缀
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //生成UUID
            String objectName = UUID.randomUUID().toString() + extension;

            //文件的请求路径
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
            return R.OK(filePath);
        } catch (IOException e) {
            return R.FAIL("文件上传失败");
        }
    }

    @PostMapping("/save")
    public R save(Shop shop) {
        // 写入数据库
        shopService.save(shop);
        // 返回店铺id
        return R.OK(shop.getId());
    }

    @DeleteMapping("/delete/{id}")
    public R deleteShop(@PathVariable("id") Long id) {
        boolean isDelete = shopService.removeById(id);
        if (isDelete) {
            return R.OK();
        }
        return R.FAIL();
    }
}
